package com.yc.service;

import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;

@Service
@Log4j
public class EmailBizImpl implements EmailBiz {

    @Value("${smtp.from}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void sendMail(String to, String subject, String context) throws Exception {

        MimeMessage mm = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mm, true,"utf-8");

        try {
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(context, true);
            javaMailSender.send(mm);
            log.info("发送邮件成功");
        }catch (Exception  e){
            e.printStackTrace();
            log.info("发送邮件失败:"+e.getMessage());
        }

    }
}
